from fastapi import APIRouter, Depends, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession

from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_kylin.entity.vo.strategy_tradebook_vo import (
    StrategyTradebookModel,
    StrategyTradebookPageQueryModel,
    DeleteStrategyTradebookModel,
)
from module_kylin.service.strategy_tradebook_service import StrategyTradebookService
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil

# 构建路由
strategyTradebookController = APIRouter(prefix='/kylin/strategyTradebook', tags=['策略交易登记簿管理'],
                                       dependencies=[Depends(LoginService.get_current_user)])


@strategyTradebookController.get('/list', response_model=PageResponseModel,
                                dependencies=[
                                    Depends(CheckUserInterfaceAuth('kylin:strategyTradebook:list'))])
async def get_strategy_tradebook_list(
        request: Request,
        tradebook_page_query: StrategyTradebookPageQueryModel = Depends(
            StrategyTradebookPageQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
        is_page: bool = True
):
    """
    获取策略交易登记簿列表
    """
    # 获取分页数据
    tradebook_page_query_result = await StrategyTradebookService.get_strategy_tradebook_list(
        query_db, tradebook_page_query, is_page
    )
    logger.info('获取策略交易登记簿列表成功')
    return ResponseUtil.success(model_content=tradebook_page_query_result)


@strategyTradebookController.get('/{tradebook_id}', response_model=StrategyTradebookModel,
                                dependencies=[
                                    Depends(CheckUserInterfaceAuth('kylin:strategyTradebook:query'))])
async def query_detail_strategy_tradebook(request: Request, tradebook_id: int,
                                         query_db: AsyncSession = Depends(get_db)):
    """
    获取策略交易登记簿详情
    """
    tradebook_detail_result = await StrategyTradebookService.get_strategy_tradebook_detail(
        query_db, tradebook_id
    )
    logger.info(f'获取策略交易登记簿{tradebook_id}的信息成功')

    return ResponseUtil.success(data=tradebook_detail_result)


@strategyTradebookController.post('', dependencies=[
    Depends(CheckUserInterfaceAuth('kylin:strategyTradebook:add'))])
@ValidateFields(validate_model='tradebook')
@Log(title='策略交易登记簿管理', business_type=BusinessType.INSERT)
async def add_strategy_tradebook(
        request: Request,
        tradebook: StrategyTradebookModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user)
):
    """
    新增策略交易登记簿
    """
    add_tradebook_result = await StrategyTradebookService.add_strategy_tradebook(
        query_db, tradebook
    )
    logger.info(f'用户{current_user.user.user_name}新增策略交易登记簿成功')

    return ResponseUtil.success(data=add_tradebook_result)


@strategyTradebookController.put('/{tradebook_id}', dependencies=[
    Depends(CheckUserInterfaceAuth('kylin:strategyTradebook:edit'))])
@ValidateFields(validate_model='tradebook')
@Log(title='策略交易登记簿管理', business_type=BusinessType.UPDATE)
async def update_strategy_tradebook(
        request: Request,
        tradebook_id: int,
        tradebook: StrategyTradebookModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user)
):
    """
    更新策略交易登记簿
    """
    update_tradebook_result = await StrategyTradebookService.update_strategy_tradebook(
        query_db, tradebook_id, tradebook
    )
    logger.info(f'用户{current_user.user.user_name}更新策略交易登记簿{tradebook_id}成功')

    return ResponseUtil.success(data=update_tradebook_result)


@strategyTradebookController.delete('', dependencies=[
    Depends(CheckUserInterfaceAuth('kylin:strategyTradebook:delete'))])
@Log(title='策略交易登记簿管理', business_type=BusinessType.DELETE)
async def delete_strategy_tradebook(
        request: Request,
        delete_model: DeleteStrategyTradebookModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user)
):
    """
    删除策略交易登记簿
    """
    delete_tradebook_result = await StrategyTradebookService.delete_strategy_tradebook(
        query_db, delete_model
    )
    logger.info(f'用户{current_user.user.user_name}删除策略交易登记簿成功')

    return ResponseUtil.success(msg=delete_tradebook_result.message)